package com.zhuhanpeng.cloud.mybatis.flexigrid;

import com.zhuhanpeng.cloud.mybatis.annotation.Column;
import com.zhuhanpeng.cloud.mybatis.entity.PtUserEntity;

import java.io.Serializable;
import java.lang.reflect.Field;

/**
 * 分页实体类
 */
public class PagingEntity implements Serializable {
    /**
     * 当前第几页
     */
    private Integer page;
    /**
     * 显示条数
     */
    private Integer rp;
    /**
     * 排序字段
     */
    private String sortname;
    /**
     * 升序还是降序
     */
    private String sortorder;

    public <T> void deal(Class<T> class1){
        if(sortname ==null || "".equals(sortname) || "undefind".equals(sortname)){
            return;
        }
        Field[] fields = class1.getDeclaredFields();
        Column column;
        String vl;
        for (Field field : fields) {
            if(sortname.equals(field.getName())){
                if(field.isAnnotationPresent(Column.class)){
                    column = field.getAnnotation(Column.class);
                    vl = column.value();
                    this.sortname = vl;
                    break;
                }
            }
        }
    }


    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRp() {
        return rp;
    }

    public void setRp(Integer rp) {
        this.rp = rp;
    }

    public String getSortname() {
        return sortname;
    }

    public void setSortname(String sortname) {
        this.sortname = sortname;
    }

    public String getSortorder() {
        return sortorder;
    }

    public void setSortorder(String sortorder) {
        this.sortorder = sortorder;
    }
    public Integer getStart() {
        return (page-1)*rp;
    }


    @Override
    public String toString() {
        return "PagingEntity{" +
                "page=" + page +
                ", rp=" + rp +
                ", sortname='" + sortname + '\'' +
                ", sortorder='" + sortorder + '\'' +
                '}';
    }

    public void dealOrder(Class<?> tClass){
        try {
            Field field=tClass.getDeclaredField(sortname);
            if (field.isAnnotationPresent(Column.class)){
                Column column=field.getAnnotation(Column.class);
                String vl = column.value();
                if (!"".equals(vl)){
                    this.sortname = vl;
                }
            }
        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
        }
    }
}